<?php
namespace User\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
//        $user = new \User\Model\UserModel();
//        $data = array("userTable"=>$user->getDbFields());
//        $datas = array("username"=>"admin","passwd"=>"123456");
//        $datas = $user->UpdatePW($datas);
//        $data["newdata"] = $datas;
//        if ($datas!=FALSE){
//            $user->create($datas);
//            $user->add();
//        }
//        $this->ajaxReturn($data);
    }
    
//    public function test(){
////        $user = new \User\Model\UserModel();
////        $data =array("test"=>$user->CheckUser(array("username"=>admin,"passwd"=>"123456")));
////        $this->ajaxReturn($data);
//        echo (int)$this->checkverify(I("request.yzm"));
//    }
    
    /**
     * 登出
     */
    public function logout(){
        session('IsLogin', NULL);
        session('uid',NULL);
    }
    /**
     * 登陆，传入user(用户名)，password(密码)，yzm（验证码）
     * 返回 
     *      json.flag:bool(T/F),
     *      json.content :String (原因/登陆成功)
     */    
    public function login(){
        if (session('IsLogin')=='1'){
            $this->ajaxReturn(array("flag"=>FALSE,"content"=>"已经登陆"));
            exit();
        }
        $request = I('request.');
        if ((isset($request["user"]))and(isset($request["yzm"]))and(isset($request["password"]))){
            if ($this->checkverify($request['yzm'])) {
                $user = new \User\Model\UserModel();
                if ( $user->CheckUser( array( "username"=>$request["user"],"passwd"=>$request["password"] ) ) ){
                    session('IsLogin', '1');
                    $condition["username"]=$request["user"];
                    $userInfo = $user->where($condition)->find();
                    session('uid',$userInfo["uid"]);
                    $this->ajaxReturn(array("flag"=>TRUE,"content"=>"登陆成功"));
                }
            }else {
                $this->ajaxReturn(array("flag"=>FALSE,"content"=>"验证码错误"));
            }
        }else{
            $this->ajaxReturn(array("flag"=>FALSE,"content"=>"未填写必填信息"));
        }
    }
    /**
     * 检查验证码
     * @param type $code 需要验证的字符串
     * @return type : bool(T/F)
     */
    private function checkverify($code){
        $verify = new \Think\Verify();
        return $verify->check($code,"");
    }
    
    /**
     * 输出验证码，直接访问即可获得验证码，大小为200*62（W*H）
     */
    public function userverify(){
        $verifyConfig = array(
            "codeSet"=>"2345689ABCDEFGHJKLMNPRSTWXY",
            "length"=>4,
            "expire"=>120,
            "useNoise"=>FALSE,
            "useCurve"=>FALSE
            );
        $verify = new \Think\Verify($verifyConfig);
        $verify->entry();
    }
    /**
     * 检查是否登陆
     * 返回 
     *      json.flag:bool(T/F),
     *      json.content :String (对应状态的字符串)
     */
    public function isLogin(){
        if (session('IsLogin')=='1'){
            $this->ajaxReturn(array("flag"=>TRUE,"content"=>"已经登陆"));
        }else{
            $this->ajaxReturn(array("flag"=>False,"content"=>"尚未登陆"));
        }
    }
    /**
     * 用于修改密码
     * 提交：
     * newpw 新密码
     * oldpw 旧密码
     * 
     */
    public function changepw(){
        $newpw = I('request.newpw',NULL);
        $oldpw = I('request.oldpw',NULL);
        if ($newpw==NULL){
            $this->ajaxReturn(array("flag"=>False,"content"=>"未提交新密码"));
            exit();
        }
        if ($oldpw==NULL){
            $this->ajaxReturn(array("flag"=>False,"content"=>"未提交旧密码"));
            exit();
        }
        if (session('IsLogin')=='1'){
            if ((session('uid')!=NULL)){
                $uid = session('uid');
                $user = new \User\Model\UserModel();
                $userinfo = $user->where(array('uid'=>$uid))->find();
                $chkdata = array('username'=>$userinfo['username'],"passwd"=>$oldpw);
                $chkpw = $user->CheckUser($chkdata);
                if (!$chkpw){
                    $this->ajaxReturn(array("flag"=>False,"content"=>"旧密码错误"));
                    exit();
                }
                $userinfo['passwd']=$newpw;
                $userinfo=$user->UpdatePW($userinfo);
                $user->save($userinfo);
                $this->ajaxReturn(array("flag"=>TRUE,"content"=>"修改密码成功"));
            }else{
                session('IsLogin', NULL);
                session('uid',NULL);
                $this->ajaxReturn(array("flag"=>False,"content"=>"尚未登陆"));
            }
        }else{
            $this->ajaxReturn(array("flag"=>False,"content"=>"尚未登陆"));
        }
    }
    
    
}